var clickValue;
layui.use(['jquery','layer'], function() {
	$ = layui.jquery;
	var layer = layui.layer;
	var _urlPath="../";
	var BDMap = (function (options) {
        function BDMap(options) {
            var defaults = {};
            this.options = $.extend(defaults, options);
            this._init();
        }

        BDMap.prototype = {
            constructor: BDMap,
            _init: function () {
                var opt = this.options,
                    map = this.map = new BMap.Map(opt.mapDiv,
                        {minZoom:9,mapType:BMAP_HYBRID_MAP});

                map.centerAndZoom(new BMap.Point(opt.center.x, opt.center.y), opt.level);
                map.addControl(new BMap.MapTypeControl());   //添加地图类型控件
                map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
            },

            getBDPoinsByJson:function (ptsJson) {
              var ol = ptsJson.data,
                i = 0, len = ol.length,
                bdPoints = [];
              for(;i<len; i++){
                bdPoints.push(new BMap.Point(ol[i][0], ol[i][1]));
              }
              return  bdPoints;
           },

            addGrid:function (pointsJson) { // 使用多边形
               var bdPoints= this.getBDPoinsByJson(pointsJson);
               var polyline = new BMap.Polyline(bdPoints,
                    {strokeColor:"blue", strokeWeight:2, strokeOpacity:1});
               this.map.addOverlay(polyline);

                // 开启编辑功能
              // polygon.enableEditing();
                // 坐标显示
               // this.polygon=polygon;
               return polyline;

            },

            addOutPolygon:function (pointsJson) { // 使用多边形
                var bdPoints= this.getBDPoinsByJson(pointsJson);
              //  { fillColor: "#fff",strokeColor:"#fff",strokeOpacity:0.00001, strokeWeight:1, fillOpacity: 0.3 });
                var polygon = new BMap.Polygon(bdPoints,
                		{ fillColor: "#fff",strokeColor:"#fff",strokeOpacity:0.00001, strokeWeight:1, fillOpacity: 0.3 });
                		//{strokeColor:"red", strokeWeight:2, strokeOpacity:1});
                this.map.addOverlay(polygon);

                 // 开启编辑功能
               // polygon.enableEditing();
                 // 坐标显示
                // this.polygon=polygon;
                return polygon;

             },
            
            removeGrid:function (overLay) {
                this.map.removeOverlay(overLay);
            },

            addBuildings:function (pointsJson) {
                var bdPoints= this.getBDPoinsByJson(pointsJson);
                var that =this;
                var markers =[];
                bdPoints.forEach(function (val,index) {
                   var  marker = new BMap.Marker(
                        val,
                        {icon: new BMap.Icon(_urlPath+"public/images/netmap/house.png",new BMap.Size(25,25))});
                    that.map.addOverlay(marker);
                    markers.push(marker);
                });
                return markers;
            },

            removeBuiding:function (makers) {
              
            },

            removeAll:function () {
                this.map.clearOverlays();
            }
        };

        return new BDMap({
            mapDiv: "allmap",
            center: {x:106.662693,y:26.565092},
            level: 17
        });
    }());

    function addGridByGridList(listGrid){
       	listGrid.forEach(function (grid) {
             BDMap.addGrid(JSON.parse(grid.polygon));
         })
    }
    
    function addOutByGridList(listGrid){
    	    listGrid.forEach(function (grid) {
         BDMap.addOutPolygon(JSON.parse(grid.polygon));
       })
    }
    
    $.getJSON(_urlPath+"admin/netmap/allMapGrid")
        .done(function (subJson) {
          	addGridByGridList(subJson);
        });
    
    
    $.getJSON(_urlPath+"admin/netmap/mapoutpolygon")
    .done(function (json) {
    	  addOutByGridList(json);
    });
    
    BDMap.map.addEventListener("click",function (e) {
        console.log(e.point.lng + "," + e.point.lat);
    });
    
    function openInfo(content,e){
		var p = e.target;
		var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
		var infoWindow = new BMap.InfoWindow(content,opts);  // 创建信息窗口对象 
		BDMap.map.openInfoWindow(infoWindow,point); //开启信息窗口
	}
    
    var opts = {
			width : 150,     // 信息窗口宽度
			height: 80,     // 信息窗口高度
			title : "信息窗口" , // 信息窗口标题
			enableMessage:true//设置允许信息窗发送短息
		   };
   
    function addBuildingByBuildingList(buildingList){
    	    buildingList.forEach(function (t) {
               var markers =  BDMap.addBuildings(JSON.parse(t.marker));
               var content=t.name;
               if (markers)
                   markers.forEach(function (marker, number) {
                       marker.addEventListener("click",function () {
//                       	$.ajax({
//                       		url : _urlPath +"admin/netmap/buildingUsers?buildingId="+t.id,
//                       		type : "post",
//                       		dataType : 'html',
//                       		success : function(req) {
//                       			$(".selectPage").remove();
//                       			$("#floor_postion").before(req);                        			                        			
//                       		},
//                       		error : function(req) {
//                       			console.log("error"+req);
//                       		}
//                       	});
                    	   clickValue=t.id;
                    	   var searchHetight = $("#searchForm").height();
                    	   var height = ($("body").height()-searchHetight)*0.6;
                    	   var width = $("body").width()*0.8;
                    	   layer.open({
                    		   type: 2, 
                    		   skin: 'layui-layer-rim', //加上边框
                    		   area: [width+'px',''+height+'px'],
                    		   content:  _urlPath+"page/netmap_floor.html"//这里content是一个普通的String
                    		 });
                           
                       });
                       marker.addEventListener("mouseover",function (e) {
                           openInfo(content,e)});
                   });

           });
    }
    
    $.getJSON(_urlPath+"admin/netmap/allMapBuilding")
        .done(function (building) {
        	   addBuildingByBuildingList(building);
        });


    $("#netMapDeleteBtn").click(function(){
        BDMap.removeBuiding();
    });

    $("#netMapShearchBtn").click(function(){
    	    var userName =$("#s_map_user").val();
    	    if(!userName){
    	    	   return;
    	    }
     	BDMap.removeAll();
        $.getJSON(_urlPath+"admin/netmap/mapshearch?userName="+userName)
            .done(function (gridAndBuildingList) {
             addGridByGridList(gridAndBuildingList.grids);
             addBuildingByBuildingList(gridAndBuildingList.buiddings);
             addOutByGridList(gridAndBuildingList.outs);
            });
    });
});

